package com.yookey.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yookey.common.constant.ResponseMessage;
import com.yookey.common.context.AccountUtils;
import com.yookey.common.exception.BusinessException;
import com.yookey.common.result.Result;
import com.yookey.common.utils.AssertUtil;
import com.yookey.common.utils.MD5Util;
import com.yookey.mapper.AccountMapper;
import com.yookey.model.dto.AccountDTO;
import com.yookey.model.dto.PasswordDTO;
import com.yookey.model.entity.Account;
import com.yookey.service.AccountService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 *
 * @author yww
 * @date 2020/9/29
 */
@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements AccountService{

    @Autowired
    private AccountMapper accountMapper;

    @Override
    public Result add(AccountDTO accountDTO) {
        Account accountByUsername = accountMapper.selectOne(new QueryWrapper<Account>().eq("username", accountDTO.getUsername()));
        AssertUtil.isNotNull(accountByUsername,"用户名已存在");
        Account accountByPhone = accountMapper.selectOne(new QueryWrapper<Account>().eq("phone", accountDTO.getPhone()));
        AssertUtil.isNotNull(accountByPhone,"手机号已存在");
        Account account = new Account();
        BeanUtils.copyProperties(accountDTO,account);
        String newPass = MD5Util.getMD5(accountDTO.getPassword());
        account.setPassword(newPass);
        account.setAvatar("https://el-admin.xin/avatar/avatar.jpeg");
        accountMapper.insert(account);
        return Result.success("新增成功");
    }

    @Override
    public Result del(Integer id) {

        int delete = accountMapper.deleteById(id);
        return Result.success("删除成功");
    }


    @Override
    public Result edit(AccountDTO accountDTO) {
        Account oldAccount = accountMapper.selectById(accountDTO.getId());
        AssertUtil.isNull(oldAccount,"用户不存在");
        AccountDTO account = AccountUtils.get();
        accountDTO.setUpdateBy(account.getUsername());
        accountDTO.setUpdateTime(new Date());
        Integer edit = accountMapper.edit(accountDTO);
        if(edit > 0){
            return Result.success("修改成功");
        }
        return Result.error("修改失败");
    }


    @Override
    public Result getPage(int page, int limit) {
        IPage<Account> iPage = new Page<>(page,limit);
        IPage<Account> result = accountMapper.selectPage(iPage, null);
        return Result.success(result);
    }

    @Override
    public Result changeState(AccountDTO accountDTO) {
        Integer integer = accountMapper.changeState(accountDTO);
        if(integer > 0) {
            return Result.success("修改成功");
        }
        return Result.error("修改失败");
    }

    @Override
    public Integer updateAvatar(String id, String path) {
        return accountMapper.updateAvatar(id,path);
    }

    @Override
    public Integer modifyPassword(PasswordDTO passwordDTO) {
        Account account = accountMapper.selectById(passwordDTO.getId());
        AssertUtil.isNull(account,"用户不存在");
        String oldPass = MD5Util.getMD5(passwordDTO.getOldPass());
        String newPass = MD5Util.getMD5(passwordDTO.getNewPass());
        if(!account.getPassword().equals(oldPass)) {

            throw new BusinessException(String.valueOf(ResponseMessage.UPDATEFAIL.getCode()),"旧密码错误");
        }
        if(account.getPassword().equals(newPass)) {
            throw new BusinessException(String.valueOf(ResponseMessage.UPDATEFAIL.getCode()),"旧密码与新密码相同");
        }
        return accountMapper.modifyPassword(account.getId(),newPass);
    }
}
